package ve.com.wayoyo.wuml_case;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class Menu_Principal extends Activity {

	private static final int FILE_SELECT_CODE = 1;	
	static final String TIPO_DIAGRAMA = null;
	private String[] values;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu__principal);
		Display display = getWindowManager().getDefaultDisplay();
		UserPref.getInstancia().setWinHeight(display.getHeight());
		UserPref.getInstancia().setWinWidth(display.getWidth());
		UserPref.getInstancia().setUsingConvections(true);
		UserPref.getInstancia().setLanguaje("Java");
		File file = new File(Environment.getExternalStorageDirectory(), "WUML//UNTITLED_PROYECT//CODIGO//");
		try {
			if ((!file.exists())&&(!file.mkdirs())) {

				Log.e("TravellerLog :: ", "Problem creating Image folder");

			}
		}catch (Exception e)
		{}
		file = new File(Environment.getExternalStorageDirectory(), "WUML//UNTITLED_PROYECT//IMG");
		try {
			if ((!file.exists())&&(!file.mkdirs())) {

				Log.e("TravellerLog :: ", "Problem creating Image folder");

			}
		}catch (Exception e)
		{}
		file = new File(Environment.getExternalStorageDirectory(), "WUML//UNTITLED_PROYECT//SCRIPTS");
		try {
			if ((!file.exists())&&(!file.mkdirs())) {

				Log.e("TravellerLog :: ", "Problem creating Image folder");

			}
		}catch (Exception e)
		{}
		file = new File(Environment.getExternalStorageDirectory(), "WUML//UNTITLED_PROYECT//DIAGRAMAS");
		try {
			if ((!file.exists())&&(!file.mkdirs())) {

				Log.e("TravellerLog :: ", "Problem creating Image folder");

			}
		}catch (Exception e)
		{}
		ListView listView = (ListView) findViewById(R.id.listView1);
		this.values = new String[] { "Android", "File Selector","BorraMemoria", "WindowsMobile",
				"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
				"Linux", "OS/2" };
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1, values);

		// Assign adapter to ListView
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 0)
					Toast.makeText(getApplicationContext(), "Android",
							Toast.LENGTH_LONG).show();
				else if(position == 1){
					//					Menu_Principal.this.showFileChooser();
					ArrayAdapter<String> adapter = new ArrayAdapter<String>(Menu_Principal.this,
							android.R.layout.simple_list_item_1, android.R.id.text1);
					FileSelector selector = new FileSelector(Menu_Principal.this,adapter);
					selector.show();
				}else
					Toast.makeText(getApplicationContext(),
							"Click ListItem Number " + position,
							Toast.LENGTH_LONG).show();
			}
		});
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_menu__principal, menu);
		return true;
	}

	public void NuevoDiagrama(View view) {
		Button btn = (Button) findViewById(R.id.button3);
		registerForContextMenu(btn);
		view.showContextMenu();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Tipo de Diagrama");
		menu.add(0, v.getId(), 0, "Diagrama de Clases");
		menu.add(0, v.getId(), 0, "Diagramas de Entidad de Relacion");
		menu.add(0, v.getId(), 0, "Grafos");
		menu.add(0, v.getId(), 0, "Otros");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Intent intent = new Intent(this, NuevoDiagrama.class);
		if (item.getTitle() == "Diagrama de Clases") {
			intent.putExtra(TIPO_DIAGRAMA, "Clases");
			startActivity(intent);
		} else if (item.getTitle() == "Diagramas de Entidad de Relacion") {
			//			intent.putExtra(TIPO_DIAGRAMA, "er");
			//			startActivity(intent);
		} else if (item.getTitle() == "Grafos") {
			//			intent.putExtra(TIPO_DIAGRAMA, "grafos");
			//			startActivity(intent);
		} else if (item.getTitle() == "Otros") {
			//			intent.putExtra(TIPO_DIAGRAMA, "otros");
			//			startActivity(intent);
		} else {
			return false;
		}
		return true;
	}

	public void AbrirDiagrama(View view) {
		ListView listView = (ListView) findViewById(R.id.listView1);
		this.values[this.values.length] = "alskdfalksjdf;lkasjdflak";
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1, values);

		// Assign adapter to ListView
		listView.setAdapter(adapter);

		// Intent intent = new Intent(this, AbrirDiagrama.class);
		// startActivity(intent);
	}

	public void nuevoProyecto(View view) {
		Intent intent = new Intent(this, NuevoProyecto.class);
		startActivity(intent);
	}

	public void compartir(View view) {
		Intent intent = new Intent(this, Compartir.class);
		startActivity(intent);
	}
}
